package yxk.service.configm;

import com.alibaba.fastjson.JSONObject;
import com.sun.javafx.binding.StringFormatter;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.workflow.engine.msg.AbstractMessageServiceHandler;
import kd.bos.workflow.engine.msg.ctx.MessageContext;
import kd.bos.workflow.engine.msg.info.ToDoInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * OA代办消息
 */
public class OAHandler extends AbstractMessageServiceHandler{
    private static final Log log = LogFactory.getLog(OAHandler.class);

    private OAService service=new OAService();
    /**
     * 流程代办
     * @param messageContext
     * @param toDoInfo
     */
    @Override
    public void createToDo(MessageContext messageContext, ToDoInfo toDoInfo) {
        log.info("messageContext:{}",messageContext);
        log.info("toDoInfo:{}",toDoInfo);
        JSONObject req=new JSONObject();
        Long processInstanceId = messageContext.getProcessInstanceId();//流程实例fid
        //获取流程信息
        DynamicObject execution = BusinessDataServiceHelper.loadSingle(processInstanceId,"wf_execution");
        String subject = execution.getString("subject");//单据名称
        String substring = subject.substring(3);
        String activityName = execution.getString("activityname");
        //获取提交人
        DynamicObject startuser = BusinessDataServiceHelper.loadSingle(execution.getLong("startuserid"), "bos_user");
        //审批人
        String flowid = "hr_yxk" + messageContext.getProcessInstanceId();

        List<String> userNames = OAService.getSelectColumByIds("bos_user", "number", toDoInfo.getUserIds());
        String receivers = userNames.toString().replace("[", "").replace("]", "").replace(" ", "");

        String url = toDoInfo.getUrl();
        StringBuffer mobUrl=new StringBuffer(url);
        mobUrl.append("&device=mob");
        StringBuffer pcUrl=new StringBuffer(url);
        pcUrl.append("&device=pc");
        log.info("createToDo.url:{}",url);
        req.put("syscode","hr_yxk");//异构系统标识
        req.put("flowid",flowid);//流程实例id
        req.put("requestname",substring);//标题
        req.put("workflowname","HR流程待办");//流程类型名称
        req.put("nodename",activityName);//节点名称
        req.put("isremark","0");//流程处理状态 0:代办；2:已办； 4:结办；
        req.put("viewtype","0");//流程查看状态0：未读  1：已读;
        req.put("creator",startuser.getString("number"));//创建人
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createdatetime = simpleDateFormat.format(new Date());
        req.put("createdatetime",createdatetime);//创建日期时间
        req.put("receivedatetime",createdatetime);//接收日期时间
        //时间戳字段，客户端使用线程调用接口的时候，根据此字段判断是否需要更新数据，防止后发的请求数据被之前的覆盖
        req.put("receivets",String.valueOf(System.currentTimeMillis()));
        String[] split = receivers.split(",");
        for (String receiver : split) {
            mobUrl.append("&userNumber="+receiver);
            pcUrl.append("&userNumber="+receiver);
            req.put("receiver",receiver);//接收人
            req.put("pcurl",pcUrl.toString());//PC地址
            req.put("appurl",mobUrl.toString());//APP地址
            log.info("OAHandler.createToDo.req:{}",req.toString());
            String requested = service.requestOAInfoBy(req.toString());
            log.info("OAHandler.createToDo.requested:{}",requested);
        }


    }
    /**
     * 流程已办
     * @param messageContext
     * @param toDoInfo
     */
    @Override
    public void dealToDo(MessageContext messageContext, ToDoInfo toDoInfo) {
        log.info("messageContext:{}",messageContext);
        log.info("toDoInfo:{}",toDoInfo);
        JSONObject req=new JSONObject();
        Long processInstanceId = messageContext.getProcessInstanceId();//流程实例fid
        //获取流程信息
        DynamicObject execution = BusinessDataServiceHelper.loadSingle(processInstanceId,"wf_execution");
        String subject = execution.getString("subject");//单据名称
        String substring = subject.substring(3);
        String activityName = execution.getString("activityname");
        String flowid = "hr_yxk" + messageContext.getProcessInstanceId();

        List<String> userNames = OAService.getSelectColumByIds("bos_user", "number", toDoInfo.getUserIds());
        String receivers = userNames.toString().replace("[", "").replace("]", "").replace(" ", "");

        //获取提交人
        DynamicObject startuser = BusinessDataServiceHelper.loadSingle(execution.getLong("startuserid"), "bos_user");
        String url = toDoInfo.getUrl();
        StringBuffer mobUrl=new StringBuffer(url);
        StringBuffer pcUrl=new StringBuffer(url);
        mobUrl.append("&device=mob");
        pcUrl.append("&device=pc");
        log.info("createToDo.url:{}",url);
        req.put("syscode","hr_yxk");//异构系统标识
        req.put("flowid",flowid);//流程实例id
        req.put("requestname",substring);//标题
        req.put("workflowname","HR流程待办");//流程类型名称
        req.put("nodename",activityName);//节点名称

        req.put("isremark","2");//流程处理状态 0:代办；2:已办； 4:结办；
        req.put("viewtype","0");//流程查看状态0：未读  1：已读;
        req.put("creator",startuser.getString("number"));//创建人
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createdatetime = simpleDateFormat.format(new Date());
        req.put("createdatetime",createdatetime);//创建日期时间

        req.put("receivedatetime",createdatetime);//接收日期时间
        //时间戳字段，客户端使用线程调用接口的时候，根据此字段判断是否需要更新数据，防止后发的请求数据被之前的覆盖
        req.put("receivets",String.valueOf(System.currentTimeMillis()));
        String[] split = receivers.split(",");
        for (String receiver : split) {
            mobUrl.append("&userNumber="+receiver);
            pcUrl.append("&userNumber="+receiver);
            req.put("receiver",receiver);//接收人
            req.put("pcurl",pcUrl.toString());//PC地址
            req.put("appurl",mobUrl.toString());//APP地址
            log.info("OAHandler.createToDo.req:{}",req.toString());
            String requested = service.requestOAInfoBy(req.toString());
            log.info("OAHandler.createToDo.requested:{}",requested);
        }
    }

    @Override
    public void deleteToDo(MessageContext messageContext, ToDoInfo toDoInfo) {
        log.info("messageContext:{}",messageContext);
        log.info("toDoInfo:{}",toDoInfo);

        String flowid = "hr_yxk" + messageContext.getProcessInstanceId();
        List<String> userNames = OAService.getSelectColumByIds("bos_user", "number", toDoInfo.getUserIds());
        String receivers = userNames.toString().replace("[", "").replace("]", "").replace(" ", "");

        String[] var20 = receivers.split(",");

        for (String s : var20) {
            JSONObject req=new JSONObject();
            req.put("syscode","hr_yxk");
            req.put("flowid",flowid);
            req.put("userid",s);
            String requestInfoBy = service.deleteUserRequestInfoBy(req.toString());
            log.info("OAHandler.deleteToDo.requestInfoBy:{}",requestInfoBy);
        }

    }



}